package com.hzy.elastic.controller;

import com.hzy.elastic.entity.QStudent;
import com.hzy.elastic.entity.Student;
import com.hzy.elastic.service.MyRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class MyRepositoryController {
    @Resource
    private MyRepository myRepository;

    public void search(@QuerydslPredicate(root = Student.class) Predicate predicate, final Pageable pageable) {
        QStudent qStudent = QStudent.student;

        if (predicate == null) predicate = new BooleanBuilder();
        // 举一例子，多级拼接
        ExpressionUtils.and(predicate, qStudent.name.like(""));
        // 执行结果
        Page<Student> all = myRepository.findAll(predicate, pageable);
        Pageable page = all.getPageable();
        List<Student> content = all.getContent();
    }
}
